home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / xlib04.zip / XBMTOOLS.ASM < prev    next >
Assembly Source File  |  1992-11-13  |  6KB  |  197 lines

  1. ;-----------------------------------------------------------------------
  2. ; MODULE XBMTOOLS
  3. ;
  4. ; Bitmap conversion / manipulation tools
  5. ;
  6. ; Compile with Tasm.
  7. ; C callable.
  8. ;
  9. ;
  10. ; ****** XLIB - Mode X graphics library                ****************
  11. ; ******                                               ****************
  12. ; ****** Written By Themie Gouthas                     ****************
  13. ;
  14. ; egg@dstos3.dsto.gov.au
  15. ; teg@bart.dsto.gov.au
  16. ;-----------------------------------------------------------------------
  17. COMMENT $
  18.  
  19.   This module implements a set of functions to manipulate both planar
  20.   bitmaps and linear bitmaps (as used by x_compile_bitmap):
  21.  
  22.   PLANAR BITMAPS
  23.  
  24.   Planar bitmaps as used by these functions have the following structure:
  25.  
  26.   BYTE 0                 The bitmap width in bytes (4 pixel groups) range 1..255
  27.   BYTE 1                 The bitmap height in rows range 1..255
  28.   BYTE 2..n1             The plane 0 pixels width*height bytes
  29.   BYTE n1..n2            The plane 1 pixels width*height bytes
  30.   BYTE n2..n3            The plane 2 pixels width*height bytes
  31.   BYTE n3..n4            The plane 3 pixels width*height bytes
  32.  
  33.   LINEAR BITMAPS
  34.  
  35.   Linear bitmaps have the following structure:
  36.  
  37.   BYTE 0                 The bitmap width in pixels  range 1..255
  38.   BYTE 1                 The bitmap height in rows   range 1..255
  39.   BYTE 2..n              The width*height bytes of the bitmap
  40.  
  41.  
  42.  
  43. $
  44.  
  45. LOCALS
  46. .286
  47.  
  48. include model.inc
  49. include xbmtools.inc
  50.  
  51.     .code
  52.  
  53.  
  54.  
  55. ;-----------------------------------------------------------------------
  56. ; x_pbm_to_bm
  57. ;
  58. ; This function converts a bitmap in the planar format to the linear format
  59. ; as used by x_compile_bitmap.
  60. ;
  61. ; WARNING: the source and destination bitmaps must be pre - allocated
  62. ;
  63. ; NOTE: This function can only convert planar bitmaps that are suitable.
  64. ;       If the source planar bitmap's width (per plane) is >= 256/4
  65. ;       it cannot be converted. In this situation an error code
  66. ;       BM_WIDTH_ERROR. On successful conversion 0 is returned.
  67. ;
  68. ; C callable as:
  69. ;    int x_pbm_to_bm(char far * source_pbm, char far * dest_bm);
  70. ;
  71. ; Written By Themie Gouthas
  72.  
  73. proc _x_pbm_to_bm
  74. ARG   src_pbm:dword,dest_bm:dword
  75.     push bp                  ; Preserve caller's stack frame
  76.     mov  bp,sp
  77.     push ds
  78.     push di
  79.     push si
  80.  
  81.     les  di,[dest_bm]     ; es:di -> destination bitmap
  82.     lds  si,[src_pbm]     ; ds:si -> source planar bitmap
  83.     lodsb                 ; load AL with source pbm pixel width per plane
  84.     mov  bl,al            ; save in CL
  85.         xor  ah,ah            ; convert to word
  86.     shl  ax,2             ; mult by 4 giving source image width
  87.     cmp  ax,255           ; if the result > 255 then we have exceeded
  88.     jl   @@WidthError     ; the max width of linear bm.
  89.  
  90.     stosb                 ; write do dest_bm
  91.  
  92.  
  93.     lodsb                 ; tranfer source pbm height in pixels to
  94.     stosb             ;  dest_bm
  95.  
  96.     xor  ah,ah            ; convert to word
  97.     mul  bl               ; AX = AX * BL ie. total no. pixels per plane
  98.     mov  dx,di            ; save DI, the pointer to the destination bm
  99.     mov  bl,3             ; set plane loop counter (BL)
  100.  
  101. @@PlaneLoop:
  102.     mov  cx,ax            ; set CX to total number of pixels per plane
  103.  
  104. @@PixelLoop:
  105.     movsb                 ; transfer pixel
  106.     add  di,3             ; increment destination to compensate for plane
  107.     loop @@PixelLoop
  108.  
  109.     inc  dx               ; increment original di for next pixel plane
  110.     mov  di,dx            ; and restore di from incremented original
  111.     dec  bl               ; decrement plane counter
  112.     jns  @@PlaneLoop      ; loop if more planes left
  113.     xor  ax,ax
  114.     jmp  short @@Done
  115. @@WidthError:
  116.     mov  ax,1
  117.  
  118. @@Done:
  119.     pop  si
  120.     pop  di
  121.     pop  ds
  122.     pop  bp                  ;restore caller's stack frame
  123.     ret
  124. _x_pbm_to_bm endp
  125.  
  126.  
  127. ;-----------------------------------------------------------------------
  128. ; x_bm_to_pbm
  129. ;
  130. ; This function converts a bitmap in the linear format as used by
  131. ; x_compile_bitmap to the planar formap.
  132. ;
  133. ; WARNING: the source and destination bitmaps must be pre - allocated
  134. ;
  135. ; NOTE: This function can only convert linear bitmaps that are suitable.
  136. ;       If the source linear bitmap's width is not a multiple of 4
  137. ;       it cannot be converted. In this situation an error code
  138. ;       BM_WIDTH_ERROR. On successful conversion 0 is returned.
  139. ;
  140. ;
  141. ; C callable as:
  142. ;    int x_bm_to_pbm(char far * source_pbm, char far * dest_bm);
  143. ;
  144. ; Written By Themie Gouthas
  145.  
  146. proc _x_bm_to_pbm
  147. ARG src_bm:dword,dest_pbm:dword
  148.     push bp                  ; Preserve caller's stack frame
  149.     mov  bp,sp
  150.     push ds
  151.     push di
  152.     push si
  153.  
  154.     les  di,[dest_pbm]       ; es:di -> destination planar bitmap
  155.     lds  si,[src_bm]         ; ds:si -> source bitmap
  156.     lodsb                    ; load AX with source bitmap width
  157.     test al,03h              ; Check that width is a multiple of 4
  158.     jnz  @@WidthIncompatible
  159.     shr  al,2                ; divide by 4 giving width of plane
  160.     stosb                    ; store destination planar bitmap width
  161.     mov  bl,al               ;  and copy to bl
  162.     lodsb
  163.     stosb            ; Transfer source bitmap height to dest pbm
  164.     xor  ah,ah               ; Conver height to word
  165.     mul  bl                  ; calculate the total no. of pixels / plane
  166.     mov  dx,si               ; save source offset
  167.     mov  bl,3
  168.  
  169. @@PlaneLoop:
  170.     mov  cx,ax            ; set CX to total number of pixels per plane
  171.  
  172. @@PixelLoop:
  173.     movsb                 ; transfer pixel
  174.     add  si,3             ; increment src offset to compensate for plane
  175.     loop @@PixelLoop
  176.  
  177.     inc  dx               ; increment original si for next pixel plane
  178.     mov  si,dx            ; and restore si from incremented original
  179.     dec  bl               ; decrement plane counter
  180.     jns  @@PlaneLoop      ; loop if more planes left
  181.     xor  ax,ax
  182.     jmp  short @@Done
  183. @@WidthIncompatible:
  184.     mov  ax,1
  185.  
  186. @@Done:
  187.     pop  si
  188.     pop  di
  189.     pop  ds
  190.     pop  bp                  ;restore caller's stack frame
  191.     ret
  192. _x_bm_to_pbm endp
  193.  
  194.  
  195.     end
  196.  
  197.